package cn.live.kyle.questionnaire;

import java.io.IOException;
import java.io.InputStream;

import cn.live.kyle.questionnaire.question.QuestionCheckPage;
import cn.live.kyle.questionnaire.question.QuestionDomParser;
import cn.live.kyle.questionnaire.question.QuestionPage;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
    private ViewGroup mViewContainer;
    private LinearLayout mProgressBar;

    private QuestionDomParser mQuestionDomParser;
    private int mCurPageIndex;
    private QuestionPage mCurQuestionPage;

    private QuestionCheckPage mQuestionCheckPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        mViewContainer = (ViewGroup) findViewById(R.id.container);
        mProgressBar = (LinearLayout) findViewById(R.id.progressBar);

        String questionFileName = "question.xml";

        try {
            InputStream is = getAssets().open(questionFileName);
            mQuestionDomParser = new QuestionDomParser();
            mQuestionDomParser.parse(is);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        setupActionBar();
        prepareProgressBar();

        mCurPageIndex = 0;
        showQuestionPage(0);
    }

    private void setupActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setTitle(mQuestionDomParser.getQuestionTitle());
    }

    private void prepareProgressBar() {
        LayoutInflater inflater = LayoutInflater.from(this);
        int count = mQuestionDomParser.getQuestionPageCount();

        for (int i = 0; i < count; i++) {
            View item = inflater.inflate(R.layout.progress_item, mProgressBar, false);
            mProgressBar.addView(item);
        }
    }

    private void setProgressBar(int index) {
        int colorS = getResources().getColor(R.color.bg_progress_s);
        int colorN = getResources().getColor(R.color.bg_progress_n);
        int count = mProgressBar.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = mProgressBar.getChildAt(i);
            if (i <= index) {
                child.setBackgroundColor(colorS);
            } else {
                child.setBackgroundColor(colorN);
            }
        }
    }

    private void showQuestionPageCheck(int index) {
        showQuestionPage(index);

        mCurQuestionPage.setPrePageButtonEnabled(false);
        mCurQuestionPage.setPrePageButtonListener(null);

        mCurQuestionPage.setNextPageButtonListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurQuestionPage.onFinshPage();
                showQuestionCheckPage();
            }
        });
        mCurQuestionPage.setNextButtonTitle(getResources().getString(
                R.string.recheck));
    }

    private void showQuestionPage(int index) {
        mViewContainer.removeAllViews();
        mCurPageIndex = index;
        mCurQuestionPage = mQuestionDomParser.createQuestionPage(mCurPageIndex,
                mViewContainer);
        mCurQuestionPage.prepareQuestionItem();
        mCurQuestionPage.preparePageTitle();
        mViewContainer.addView(mCurQuestionPage.getPageView());

        if (mCurPageIndex - 1 >= 0) {
            mCurQuestionPage.setPrePageButtonEnabled(true);
            mCurQuestionPage
                    .setPrePageButtonListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mCurQuestionPage.onFinshPage();
                            showQuestionPage(mCurPageIndex - 1);
                        }
                    });
        } else {
            mCurQuestionPage.setPrePageButtonEnabled(false);
            mCurQuestionPage.setPrePageButtonListener(null);
        }

        if (mCurPageIndex + 1 < mQuestionDomParser.getQuestionPageCount()) {
            mCurQuestionPage
                    .setNextPageButtonListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mCurQuestionPage.onFinshPage();
                            showQuestionPage(mCurPageIndex + 1);
                        }
                    });
            mCurQuestionPage.onQuestionItemStateChanged();
        } else {
            mCurQuestionPage
                    .setNextPageButtonListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mCurQuestionPage.onFinshPage();

                            // mQuestionDomParser.generate(QuestionActivity.this);
                            // finish();

                            showQuestionCheckPage();
                        }
                    });
            mCurQuestionPage.onQuestionItemStateChanged();
        }

        setProgressBar(mCurPageIndex);
    }

    private void showQuestionCheckPage() {
        if (mQuestionCheckPage == null) {
            mQuestionCheckPage = mQuestionDomParser
                    .createQuestionCheckPage(mViewContainer);
        }

        mViewContainer.removeAllViews();
        mViewContainer.addView(mQuestionCheckPage.getCheckPageView());

        mQuestionCheckPage.setPrePageButtonListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showQuestionPage(mQuestionDomParser.getQuestionPageCount() - 1);
            }
        });

        mQuestionCheckPage
                .setNextPageButtonListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        mQuestionDomParser.generate(MainActivity.this);
                        finish();
                    }
                });

        mQuestionCheckPage
                .setOnQuestionClicked(new QuestionCheckPage.OnQuestionClicked() {
                    @Override
                    public void onClick(int pageIndex) {
                        showQuestionPageCheck(pageIndex);
                    }
                });
        
        setProgressBar(mQuestionDomParser.getQuestionPageCount() - 1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.question, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
